﻿using IJhbyService;
using JhbyCommon.CommonModel;
using JhbyHelper;
using JhbyModel.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace jhbyDemo.ApiControllor
{
    [ApiController]
    [Route("api/[controller]")]
    public class LoginController : ControllerBase
    {
        //注入
        ILoginService _loginService;
        public LoginController(ILoginService loginService)
        {
            _loginService = loginService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Login")]
        public string Login(string userno, string password)
        {
             
            this.HttpContext.Request.EnableBuffering();
            Response.Userinfos loginfo = (Response.Userinfos)_loginService.Login(userno, password);
            if (loginfo.Code == 0)
            {
                //生成token
                loginfo.AuthorizeToken = TokenHelper.CreateToken(loginfo);
            }
           return JsonConvert.SerializeObject(loginfo);
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        [HttpGet]
        [Route("LogOut")]
        public string LogOut()
        {
            return _loginService.LogOut();
        }
    }
}
